//
//  UUWalletListVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 24/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUWalletListVC.h"
#import "UUBaseTableViewCell.h"
#import "UUWalletManager.h"
#import "UUWalletWithdrawDetailVC.h"
#import "UUOrderDetailVC.h"
#import "UUWalletRefundDetailVC.h"

@interface UUWalletListCell : UUBaseTableViewCell

@property (nonatomic, weak) IBOutlet UILabel *typeLabel;
@property (nonatomic, weak) IBOutlet UILabel *timeLabel;
@property (nonatomic, weak) IBOutlet UILabel *moneyLabel;

@end

@implementation UUWalletListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.timeLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
}

- (void)setupWithModel:(UUWalletRecordModel *)model {
    self.timeLabel.text = [model.createTime formatDateByFormatString:@"yyyy.MM.dd HH:mm:ss"];
    self.typeLabel.text = model.remark;
    
    switch (model.type) {
        case UUWalletRecordTypeWithdraw:
            self.moneyLabel.text = [NSString stringWithFormat:@"-￥%.2f", model.money.unsignedIntegerValue / 100.0];
            self.moneyLabel.textColor = RGB_COLOR(131, 184, 73);
            break;
        case UUWalletRecordTypeIncome:
            self.moneyLabel.text = [NSString stringWithFormat:@"+￥%.2f", model.money.unsignedIntegerValue / 100.0];
            self.moneyLabel.textColor = COMMON_TEXT_RED_COLOR;
            break;
        default:
            DKAssert(0);
    }
}

@end

////////////////////////////////////////////////////////////

#define PAGE_LENGTH     (20)

@interface UUWalletListVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) IBOutlet UITableView *tableView;

@property (nonatomic, assign) UUWalletRecordType type;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray<UUWalletRecordModel *> *records;

@end

@implementation UUWalletListVC

+ (instancetype)listWithType:(UUWalletRecordType)type {
    UUWalletListVC *listVC = [self instanceOfAppropriate];
    listVC.type = type;
    
    return listVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.backgroundColor = COMMON_BACKGROUND_COLOR;
    
    WEAK_SELF();
    self.tableView.mj_footer = [UURefreshFooter footerWithRefreshingBlock:^{
        [weakSelf loadMore];
    }];
    self.tableView.mj_footer.hidden = YES;
    
    self.tableView.mj_header = [UURefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadForFirst];
    }];
 
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadForFirst {
    WEAK_SELF();
    [GetWalletManager() asyncFetchRecordsForType:self.type
                                            page:1
                                          length:PAGE_LENGTH
                                   completeBlock:^(NSArray<UUWalletRecordModel *> *records, NSError *error) {
                                       [weakSelf.tableView.mj_header endRefreshing];
                                       
                                       if (error == nil) {
                                           weakSelf.records = [records mutableCopy];
                                           [weakSelf.tableView reloadData];
                                           
                                           weakSelf.page = 2;
                                           weakSelf.tableView.mj_footer.hidden = records.count < PAGE_LENGTH;
                                       } else {
                                           ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                       }
                                   }];
}

- (void)loadMore {
    WEAK_SELF();
    [GetWalletManager() asyncFetchRecordsForType:self.type
                                            page:self.page
                                          length:PAGE_LENGTH
                                   completeBlock:^(NSArray<UUWalletRecordModel *> *records, NSError *error) {
                                       HideIndicatorInView(weakSelf.view);
                                       
                                       if (error == nil) {
                                           if (records.count < PAGE_LENGTH) {
                                               [weakSelf.tableView.mj_footer endRefreshing];
                                               weakSelf.tableView.mj_footer.hidden = YES;
                                           } else {
                                               [weakSelf.tableView.mj_footer endRefreshing];
                                           }
                                           
                                           ++weakSelf.page;
                                           [weakSelf.records addObjectsFromArray:records];
                                           [weakSelf.tableView reloadData];
                                       } else {
                                           [weakSelf.tableView.mj_footer endRefreshing];
                                           ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                       }
                                   }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UUWalletListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    UUWalletRecordModel *record = self.records[indexPath.row];
    [cell setupWithModel:record];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UUWalletRecordModel *record = self.records[indexPath.row];
    
    if ([record.target isEqualToString:@"order"]) {
        UUOrderDetailVC *detailVC = [UUOrderDetailVC instanceOfAppropriate];
        detailVC.orderId = record.targetId;
        [self.navigationController pushViewController:detailVC animated:YES];
    } else if ([record.target isEqualToString:@"withdraw"]) {
        UUWalletWithdrawDetailVC *detailVC = [UUWalletWithdrawDetailVC instanceOfAppropriate];
        detailVC.recordId = record.targetId;
        [self.navigationController pushViewController:detailVC animated:YES];
    } else if ([record.target isEqualToString:@"refund"]) {
        UUWalletRefundDetailVC *detailVC = [UUWalletRefundDetailVC instanceOfAppropriate];
        detailVC.recordId = record.targetId;
        detailVC.moneyLogId = record.id;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
